package com.example.zyl.api.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.zyl.api.entity.SysUser;
import com.example.zyl.api.entity.dto.SysUserDto;
import com.example.zyl.api.service.SysUserService;
import com.example.zyl.common.CommonResult;
import com.example.zyl.common.query.annotation.ApiLimit;
import com.example.zyl.common.query.annotation.ApiLock;
import com.example.zyl.common.query.annotation.ApiLog;
import com.example.zyl.common.query.enums.BusinessType;
import com.example.zyl.common.query.page.PageParam;
import com.example.zyl.common.query.page.PageResult;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户信息表(SysUser)表控制层
 *
 * @author zyl
 * @since 2024-12-02 09:19:27
 */

@RestController
@RequestMapping("sysUser")
@RequiredArgsConstructor
public class SysUserController {
    /**
     * 服务对象
     */
    private final SysUserService sysUserService;
    
    /**
     * 分页查询
     *
     * @param pageParam 筛选条件
     * @return 查询结果
     */
    @GetMapping(value = "/queryByPage")
    @ApiLog(module = "查询用户信息表列表", businessType = BusinessType.SELECT)
    public PageResult<List<SysUser>> queryByPage(@RequestBody PageParam<SysUser, SysUserDto> pageParam) {
        Page<SysUser> page = this.sysUserService.queryByPage(pageParam);
        return PageResult.pageResult(page);
    }


    @PostMapping(value = "/update")
    @ApiLock
    public CommonResult<Object> update() {
        return CommonResult.supplier(sysUserService.updateLock()).get();
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    @ApiLog(module = "查询用户信息表单条", businessType = BusinessType.SELECT)
    @ApiLimit
    public SysUser queryById(@PathVariable("id") Long id) {
        return this.sysUserService.queryById(id).get();
    }

    /**
     * 新增数据
     *
     * @return 新增结果
     */
    @PostMapping
    @ApiLog(module = "新增用户信息表", businessType = BusinessType.INSERT)
    public void add() {
        sysUserService.insert(new SysUser());
    }

    /**
     * 编辑数据
     *
     * @param sysUser 实体
     * @return 编辑结果
     */
    @PutMapping
    @ApiLog(module = "编辑用户信息表", businessType = BusinessType.UPDATE)
    public CommonResult<SysUser> edit(SysUser sysUser) {
        return CommonResult.supplier(this.sysUserService.update(sysUser)).get();
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    @ApiLog(module = "删除用户信息表", businessType = BusinessType.DELETE)
    public CommonResult<Boolean> deleteById(Long id) {
        return CommonResult.supplier(this.sysUserService.deleteById(id)).get();
    }


    /**
     * 批量导出excel
     *
     */
    @GetMapping(value = "/export")
    @ApiLog(module = "导出用户信息表", businessType = BusinessType.EXPORT)
    public void export(@RequestBody PageParam<SysUser, SysUserDto> pageParam) {
        this.sysUserService.export(pageParam);
    }

}

